package com.feizi.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.feizi.entity.UserEntity;
import com.feizi.service.LoginService;

@Controller
public class LoginController {
   @Autowired
   private LoginService loginService;
   @RequestMapping(path = "/login.do",method = RequestMethod.GET)
   public String login(HttpServletRequest request,UserEntity userEntity,HttpSession session,Model model,String YZM) {
		UserEntity user = loginService.login(userEntity);
		session.setAttribute("user", user);
		String session_vcode=(String) request.getSession().getAttribute("text");
		
		if (user!=null && session_vcode.equalsIgnoreCase(YZM)) {
			return "redirect:/findProduct.do";
		}else {
			model.addAttribute("msg", "登录失败，请核对密码或账号后再登录");
		return "login";
		}
   }
   @RequestMapping(path = "/quit.do",method = RequestMethod.GET)
   public String quit(HttpSession session) {
	   	session.setAttribute("user", session.getAttribute("users"));
		return "login";
	}
   @RequestMapping(path = "/changePassword.do",method = RequestMethod.GET)
   public String changePassword(HttpServletRequest request,UserEntity userEntity,HttpSession session,Model model,String YZM,String jPassword) {
		UserEntity user=(UserEntity) session.getAttribute("user");
		String session_vcode=(String) request.getSession().getAttribute("change");
		userEntity.setUserid(user.getUserid());
		if(!user.getPassword().equals(jPassword)) {
			model.addAttribute("msg", "原密码错误");
			return "remima";
		}else if(!userEntity.getPassword().equals(userEntity.getPasswords())) {
			model.addAttribute("msg", "两次输入的密码不一致");
			return "remima";
		}else if(!session_vcode.equalsIgnoreCase(YZM)) {
			model.addAttribute("msg", "验证码错误");
			return "remima";
		}else {
			int rows=loginService.changePassword(userEntity);
			if(rows>0) {
				session.setAttribute("user", session.getAttribute("users"));
				model.addAttribute("msg", "密码修改成功，请重新登录");
				return "login";
			}else {
				model.addAttribute("msg", "密码修改错误");
				return "remima";
			}
		}
  }
   
}
